#include <QGuiApplication>
#include <QQuickView>
#include "CCppData.h"
#include <QQmlContext>
#include <QQmlApplicationEngine>
#include "CCppImg.h"

/* 这个示例程序演示了QML如何展示C++中生产数据
 * 1.访问C++对象CCppData提供的常规数据
 * 2.访问C++对象CCppImg提供的图片
*/
int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);



    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);

    //1.为QML设置一个QObject的上下文，在QML中可以通过cppData来使用data这个实例，可以绑定信号、访问函数
    CCppData data;
    engine.rootContext()->setContextProperty("cppData",&data);

    //2.添加一个图片源
    engine.addImageProvider("img",new CCppImg);
    engine.load(url);

    return app.exec();
}
